/*
 * This is source for Record class
 */
package journalarray;
import java.util.*;

enum RecordPriority {
    LOW,
    MEDIUM,
    HIGH,
    HIGHEST
}

/**
 * Record class
 * This class use for save record with fields:
 *  - date / time
 *  - priority (type RecordPriority)
 *  - source (lines without spaces)
 *  - message (lines with spaces, but without line feed)
 * @author Евгений Калиниченко
 */
public final class Record {
    private Date date;
    private RecordPriority priority;
    private String source;
    private String message;
    
    /**
     * Constructor for class record
     * example you can create class with initial value
     * @param date date and time of action
     * @param priority priority of actions (1..4 level)
     * @param source source of actions (String without speces)
     * @param message main information about actions (String with spaces but without line feed)
     */
    public Record(Date date, RecordPriority priority, String source, String message) {
        this.date = date;
        this.priority = priority;
        this.source = source;
        this.message = message;
    }
    
    /**
     * Constructor for class record
     * init class with initial string - initStr
     * Initial string format:
     * yyyy-mm-dd hh:mm:ss Importance Source Message
     * yyyy - year
     * mm - mounth
     * dd - day of mounth
     * hh - hours (24h)
     * mm - minutes
     * ss - seconds
     * Importance - Importance of actions, have next format: ".","!","!!" or "!!!"
     * Source - string without spaces
     * Message - string with spaces but without line feed
     * Example: Record("2013-01-31 22:04:33 !!! Keyboard Keyboard error")
     * @param initStr initial string
     * @exception IllegalArgumentException if initStr is not have valid format, see valid format for init string
     */
    public Record(String initStr) {
        //Test input string
        if (!initStr.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} +[0-9]{2}:[0-9]{2}:[0-9]{2} +(.|!|!!|!!!) +[A-Za-zА-Яа-яЁ\\S]+ +([A-Za-zА-Яа-яЁ]| |\\S)+")) {
            throw new IllegalArgumentException();
        }
        //Split string to string array
        String [] parseStr = initStr.split(" +", 5);
        
        
        
        String [] date = parseStr[0].split("-");
        String [] time = parseStr[1].split(":");
        
        
        
        Calendar calendar = new GregorianCalendar();
        
        calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1,
                Integer.parseInt(date[2]), Integer.parseInt(time[0]),
                Integer.parseInt(time[1]), Integer.parseInt(time[2]));
        
        this.date = calendar.getTime();
        
        this.priority = this.getPriorityByString(parseStr[2]);
        
        this.source = parseStr[3];
        this.message = parseStr[4];
        
    }
    
    private RecordPriority getPriorityByString(String s) {
        if (s.equals(".")) return RecordPriority.LOW;
        if (s.equals("!")) return RecordPriority.MEDIUM;        
        if (s.equals("!!")) return RecordPriority.HIGH;        
        if (s.equals("!!!")) return RecordPriority.HIGHEST;
        
        return null;
    }
    
    private String priorityToString(RecordPriority p) {
        switch (p) {
            case LOW:     return ".   ";
            case MEDIUM:  return "!   ";
            case HIGH:    return "!!  ";
            case HIGHEST: return "!!! ";
        }
        
        return null;
    }
    /**
     * Return information of record
     * Example: "2013-01-31 22:04:33 !!! Keyboard Keyboard error"
     * @return 
     */
    public String toString() {
        String retStr = new String();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(this.date);
        
        retStr = retStr.concat(String.valueOf(calendar.get(Calendar.YEAR)) + "-" + 
                (((calendar.get(Calendar.MONTH) + 1) < 10) ? "0" : "") + String.valueOf(calendar.get(Calendar.MONTH) + 1) + "-" + 
                String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + " " + String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + ":" +
                (calendar.get(Calendar.MINUTE) < 10 ? "0" : "") + String.valueOf(calendar.get(Calendar.MINUTE)) + ":" + 
                String.valueOf(calendar.get(Calendar.SECOND)) + " " +
                priorityToString(this.priority) + " " + this.source + " " + this.message);
        
        return retStr;
    }
    
    /**
     * Return time of the current record
     * @return time of the current record
     */
    public Date getTime() {
        return this.date;
    }
    
    /**
     * Return importance of current record
     * @return importance of current record
     */
    public RecordPriority getImportance() {
        return priority;
    }
    
    /**
     * Return field source of current record
     * @return source of the record
     */
    public String getSource() {
        return source;
    }
    
    /**
     * Return field message of current record
     * @return message of the record
     */
    public String getMessage() {
        return message;
    }
}
